#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;

const int N = 2e6 + 10;
const int M = 2e6 + 10;
int h[M], e[N], ne[N], idx, d[M];
void add(int a, int b)
{
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}
void solve()
{

    idx = 0;
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
    {
        h[i] = -1;
        d[i] = 0;
    }
    vector<string> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        string s = " " + a[i];
        for (int j = i + 1; j <= n; j++)
        {
            if (s[j] == '1')
            {
                add(i, j);
                d[j]++;
            }
        }
    }
    priority_queue<int> heap;
    for (int i = 1; i <= n; i++)
    {
        if (d[i] == 0) heap.push(i);
    }
    while (heap.size())
    {
        int ver = heap.top();
        cout << ver << " ";
        heap.pop();
        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (--d[j] == 0) heap.push(j);
        }
    }
    cout << endl;
}
signed main()
{
    int tt = 1;
    cin >> tt;
    while (tt--)solve();
    return 0;
}